package keter.framework.util;

import keter.framework.exception.runtime.KeterValidateException;
import keter.framework.model.base.BaseModel;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ValidateUtil{
    
    private BaseModel m;
  //类加载时就进行实例化
    private static ValidateUtil m_instance = new ValidateUtil();
    //构造方式私有化，保证外界无法直接实例化
    private ValidateUtil(){
        
    }
    
    public ValidateUtil(BaseModel model){
        this.m = model;
    }
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(ValidateUtil.class);
 
    public  ValidateUtil notEmpty(String attr){
        try {
            Validate.notEmpty(m.getStr(attr).trim());
        } catch (Exception e) {
            throw new KeterValidateException("校验失败:内容不能为空!");
        }
        return this;
    }
    
    public ValidateUtil min(String attr, int minLen){
        length(attr,minLen,Integer.MAX_VALUE);
        return this;
    }
    
    public ValidateUtil max(String attr, int minLen){
        length(attr,minLen,Integer.MAX_VALUE);
        return this;
    }
    
    public ValidateUtil length(String attr, int minLen, int maxLen){
        String value = m.getStr(attr);
        if (value == null || value.length() < minLen || value.length() > maxLen){
            throw new KeterValidateException("校验失败:内容长度不符合要求");
        }
        return this;
    }
}
